<?php
namespace Home\Controller;
use Think\Controller;
echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">';

class UserCenterController extends Controller {

    public function __construct()
    {
        parent::__construct();

        $Wechat = A('Wechat'); //调用微信控制器
        //$Wechat->definedItem();//自定义菜单 调用一次创建就行了
        $user_id = session('user_id');
        if(empty($user_id))
        {
            $Wechat->getWc_user();
        }
        
    }

    public function register()
    {

    	if(!isset($_POST['act_register']))
    	{
            $region = D('Region');
            $reg_list = $region ->get_region(1);
            $this->assign('reg_list',$reg_list);

    		$this->display();
    	}
    	else
    	{
    		$res['mobile_phone'] = I('post.mobile_phone','');
    		$res['user_name']    = I('post.user_name','');
    		$res['sex']          = I('post.sex','');
            $res['birthday']     = I('post.bir_year','').'-'.I('post.bir_month','').'-'.I('post.bir_day','');
            $res['car_no']       = I('post.car_no','');
            $res['province']     = I('post.province','');
            $res['city']         = I('post.city','');
    		$res['district']     = I('post.district','');

    		$users = D('Users');
            $user_id = $users->get_register($res);
            //dump($user_id);
            if($user_id)
            {
                session('user_id',$user_id);  
                session('user_name',$res['user_name']);
                $this->success('注册成功，返回首页',U('Index/index'),3);
            }
            else
            {
                $this->error('注册失败，请重新注册',U('Register/register'),5);
            }
    	}
        
    }
    //商家注册
    public function shop()
    {
        $user_id = session('user_id') ? session('user_id') : 1;
        if(!isset($_POST['act_shop']))
        {
            $shop = D('Shop');
            $list = $shop->shopsw($user_id);
            $this->assign('list',$list);
            $this->display();
        }
        else
        {
            $id                      = I('post.id','');
            $res['floorid']          = I('post.floorid','');
            $res['name']             = I('post.name','');
            $res['summary']          = I('post.summary','');
            $res['address']          = I('post.address','');
            $res['tel']              = I('post.tel','');
            
            $res['content']          = I('post.content','');
            $res['userid']           = $user_id;
            $res['starttime']        = I('post.starttime','');
            $res['endtime']          = I('post.endtime','');
            $res['shopnum']          = I('post.shopnum','');
            $res['createtime']          = date('Y-m-d H:i:s');

            $shop = D('Shop');
            $shop->shopreg($res,$id);
            $this->success('成功，返回首页',U('Index/index'),3);
        }
    }
    //我的活动
    public function activity()
    {
        $id                      = I('get.id',0);
        $user_id = session('user_id') ? session('user_id') : 1;
        $acti = D('Activity');
        $list = $acti->activitylist($user_id,$id);
        $this->assign('list',$list);
        $this->assign('id',$id);
        $this->display();
    }

    //微信登录
    public function login($wchat)
    {
        //通过openid查询数据库的openid是否存在 如果存在就返回user_id，不存在就添加
        $users = D('Users');
        $user_id = $users->find_openid($wchat['openid']);
        if($user_id)
        {
            session('user_id',$user_id);  
            session('user_name',$wchat['nickname']);
            //回调地址
            //$pageURL = $_SERVER["REQUEST_URI"];
            //$str ='/Home/UserCenter/member'; 
            //preg_match("/([\/]?Home[\/]?+)([\/]?[\w\/\?=&;%@#\+,]+)/i",$pageURL,$ulr); 
            //print_r($ulr); 

            //更新登录时间
            $users->logintime = date('Y-m-d H:i:s');
            $users->where('user_id='.$user_id)->save();

            $this->success('登录成功，返回',U('Index/index'),1);
            //$this->success('登录成功，返回首页', U($ulr(2)), 3);
        }
        else
        {
            $res['openid']       = $wchat['openid'];
            $res['user_name']    = $wchat['nickname'];
            $res['sex']          = $wchat['sex'];
            $res['avatar']       = $wchat['headimgurl'];
            $res['createtime']   = date('Y-m-d H:i:s');
            $res['logintime']    = date('Y-m-d H:i:s');

            $user_id = $users->get_register($res);
            if($user_id)
            {
                session('user_id',$user_id);  
                session('user_name',$wchat['nickname']);
                $this->success('登录成功，返回首页',U('Index/index'),1);
            }
        }
    }

    public function member()
    {
        $user_id = session('user_id') ? session('user_id') : 1;//暂时代替，做微信登录时修改
        //echo "看看".$user_id;//怎么变成数组了呢？

        if($user_id)
        {
            $users = D('Users');
            $user_info = $users->get_userinfo($user_id);
            $this->assign('u_info',$user_info);
            $this->display();
        }
        else
        {
            $this->error('请先登录',U('Index/index'),3);
        }
        
    }

    public function my_card()
    {
        $user_id = session('user_id') ? session('user_id') : 1;//暂时代替，做微信登录时修改
        if($user_id)
        {
            $types = D('CardType');
            $card_type = $types->select();
            $this->assign('card_type',$card_type);
            $this->display();
        }
        else
        {
            $this->error('请先登录',U('Index/index'),3);
        }
    }
    public function own_card()
    {
        $user_id    = session('user_id') ? session('user_id') : 1;//暂时代替，做微信登录时修改
        $type_id    = I('get.type_id','');
        $is_status  = I('get.sid',1);

        $user = D('UserCard');
        $own_list = $user->get_usercard($type_id,$user_id,$is_status);

        $type = D('CardType');
        $type_name = $type->get_typename($type_id);

        $this->assign('type_name',$type_name);

        $this->assign('own_list',$own_list);
        $this->assign('tid',$type_id);
        $this->display();
    }
    //使用卡券
    public function goto_use()
    {
        $user_id     = session('user_id') ? session('user_id') : 1;
        $ucrd_id     = I('get.uid',0);

        $cards       = D('UserCard');
        $obtains     = $cards -> obtain_card($ucrd_id,$user_id);
        $this->assign('obtains',$obtains);
        $this->display();
    }
    //我的积分
    public function my_points()
    {
        $page     = I('get.p',1);
        $user_id    = session('user_id') ? session('user_id') : 1;//暂时代替，做微信登录时修改

        $users = D('Users');
        $user_info = $users->get_userinfo($user_id);
        $this->assign('u_info',$user_info);

        //积分记录
        $potlog = D('PointsLog');
        $plist = $potlog->points_list($user_id,$page);
  
        $this->assign('polst',$plist['polst']);
        $this->assign('page', $plist['show']);// 赋值分页输出

        $this->display();

    }
    //ajax加载积分
    public function more_points()
    {
        $page     = I('get.page',1);
        $user_id    = session('user_id') ? session('user_id') : 1;
        if(IS_AJAX)
        {  
            $num  = 3;//请求条数
            $list = D('PointsLog')->where('user_id='.$user_id)->page($page,$num)->select();    
            $count = count($list);
            if($count<$num)
            {//判断是否到尾页
                $list[]['log_id']=0;//到尾页返回0
            }
            $this->ajaxReturn($list);//将结果返回给前台  
        }
    }

    //个人信息
    public function my_info()
    {
        $user_id    = session('user_id') ? session('user_id') : 1;//暂时代替，做微信登录时修改

        $users = D('Users');
        $user_info = $users->get_userinfo($user_id);
        $this->assign('u_info',$user_info);

        $region = D('Region');
        $reg_list = $region ->get_region(1);
        $this->assign('reg_list',$reg_list);

        $citys = $region ->get_area($user_info['province']);
        $this->assign('citys',$citys);

        $districts = $region ->get_area($user_info['city']);
        $this->assign('districts',$districts);

        //截取字符串 分隔符可以是斜线，点，或横线 
        //list($year, $month, $day) = split ('[/.-]', $user_info['birthday']); 
        //echo "year: $year; month: $month; day: $day<br />\n"; 

        $this->display();
    }
    //更新会员信息
    public function modify_info()
    {
        $user_id    = session('user_id') ? session('user_id') : 1;

        $res['mobile_phone'] = I('post.mobile_phone','');
        $res['user_name']    = I('post.user_name','');
        $res['sex']          = I('post.sex','');
        $res['birthday']     = I('post.bir_year','').'-'.I('post.bir_month','').'-'.I('post.bir_day','');
        $res['car_no']       = I('post.car_no','');
        $res['province']     = I('post.province','');
        $res['city']         = I('post.city','');
        $res['district']     = I('post.district','');

        $users = D('Users');
        $user_id = $users->where('user_id='.$user_id)->save($res);

        $this->success('编辑成功，返回会员中心',U('UserCenter/member'),3);
    }

    //联系我们
    public function link_us()
    {
        $link = D('LinkUs');
        $link_us = $link->find();
        $this->assign('link_us',$link_us);
        $this->display();
    }

    //分享amall优惠活动
    public function share()
    {
        $Wechat = A('Wechat'); //调用微信控制器
        $share = $Wechat->sharewx();

        $this->assign('timestamp', $share['timestamp']);
        $this->assign('nonceStr',  $share['nonceStr']);
        $this->assign('signature', $share['signature']);
        $this->display();
    }
    //成功分享返回增加积分
    public function up_points()
    {
        $data = array();
        //后台share_type表分享amall优惠活动 t_id==1，由于微信分享比较特殊,我这里直接等于1
        $user_id = session('user_id');
        $pointmodel = D('PointsLog');
        $log_text = '分享amall优惠';
        //改变积分
        $log_id = $pointmodel->change_points(1,$user_id, 1, $log_text);

        $data['status'] = 1;   
        $data['info'] = 'ajax成功';  
        $data['url'] = U('UserCenter/share');
        

        //通过ajaxReturn()方法返回我们之前生成的数组  
        $this->ajaxReturn($data);  
    }
    public function sharecode()
    {
        $Wechat = A('Wechat'); //调用微信控制器
        $code_img = $Wechat->shareqr();
        $this->assign('img',$code_img);
        $this->display();
    }
}